package remote.api;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

public class FetchRemote {

	private JSONParser jsonParser;
	private static String URL = "http://square.ufba.br/jsonapi/?";
	private static String PALAVRACHAVE = "abracadabra";
	private static String TOKEN;
	private static String URLN;

	// constructor
	public FetchRemote(){
		jsonParser = new JSONParser();
		
		TOKEN = "token="+md5(PALAVRACHAVE);
	}

	// LOGIN
	public JSONObject login(String email,String pass){
		String tag = "&tag=login";
		URLN = URL + TOKEN + tag;

		// Building Parameters
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("email", email));
		params.add(new BasicNameValuePair("password", pass));
		JSONObject json = jsonParser.getJSONFromUrl(URLN, params);
		return json;
	}
	
	// NOVO CADASTRO
	public JSONObject novocadastro(String nome,String email,String pass){
		String tag = "&tag=novocadastro";
		URLN = URL + TOKEN + tag;

		// Building Parameters
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("nome", nome));
		params.add(new BasicNameValuePair("email", email));
		params.add(new BasicNameValuePair("password", pass));
		JSONObject json = jsonParser.getJSONFromUrl(URLN, params);
		return json;
	}

	// LOGIN NO FACE
		public JSONObject loginFacebook (String nome, String email){
			String tag = "&tag=loginface";
			URLN = URL + tag;

			// Building Parameters
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("nome", nome));
			params.add(new BasicNameValuePair("email", email));
			JSONObject json = jsonParser.getJSONFromUrl(URLN, params);
			//System.out.println(json);
			return json;
		}
		
		
		

	// POPULATE
	public JSONObject populate(){
		String tag = "&tag=populate";
		URLN = URL + TOKEN + tag;

		// Building Parameters
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("registro", "1"));
		JSONObject json = jsonParser.getJSONFromUrl(URLN, params);
		return json;
	}

	// SINCRONIZANDO OCORRENCIAS
		public JSONObject syncfalhas(String iduser,String falhas){
		String tag = "&tag=syncfalhas";
		URLN = URL + TOKEN + tag;

		// Building Parameters
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("iduser", iduser));
		params.add(new BasicNameValuePair("falhas", falhas));
		//System.out.println(iduser);
		//System.out.println(falhas);
		JSONObject json = jsonParser.getJSONFromUrl(URLN, params);
		return json;
	}

	public static String md5(String string) {
		byte[] hash;

		try {
			hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException("Huh, MD5 should be supported?", e);
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException("Huh, UTF-8 should be supported?", e);
		}
		
		StringBuilder hex = new StringBuilder(hash.length * 2);
		
		for (byte b : hash) {
			int i = (b & 0xFF);
			if (i < 0x10) hex.append('0');
			hex.append(Integer.toHexString(i));
		}

		return hex.toString();
	}
}